<template>
  <div class="ry-perList">
    <!-- 顶部搜索 -->
    <div class="head">
      <el-form :inline="true" :model="perSearchForm" class="demo-form-inline">
        <el-form-item label="人员搜索：">
          <el-input v-model="perSearchForm.userName" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search" icon="el-icon-search"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <!-- 顶部搜索 -->

    <!-- 列表区域 -->
    <div class="body">
      <!-- 新建按钮 -->
      <el-button icon="el-icon-circle-plus-outline" @click="isShow=true">新建</el-button>
      <!-- 新建按钮 -->

      <!-- 列表 -->
      <div class="list">
        <el-table :data="personList" style="width: 100%">
          <el-table-column prop="num" label="序号" width="80" type="index">
          </el-table-column>
          <el-table-column prop="userName" label="人员名称" width="250">
          </el-table-column>
          <el-table-column prop="regionName" label="归属区域" width="260">
          </el-table-column>
          <el-table-column prop="role" label="角色" width="260">
          </el-table-column>
          <el-table-column prop="mobile" label="联系电话" width="260">
          </el-table-column>
          <el-table-column prop="handle" label="操作">
            <template slot-scope="scope">
              <a href="javascript:;" class="change">修改</a>
              <a href="javascript:;" class="del" @click="delPerson(scope.row.id)">删除</a>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 列表 -->
    </div>
    <!-- 列表区域 -->

    <!-- 底部 -->
    <div class="foot">
      <span
        >共{{ totalCount }}条记录 第 {{ pageIndex }}/{{ totalPage }} 页</span
      >
      <div class="but">
        <el-button @click="prePage" :disabled="preBtn">上一页</el-button>
        <el-button @click="nextPage" :disabled="nextBtn">下一页</el-button>
      </div>
    </div>
    <!-- 底部 -->
    <addPerson :isShow="isShow" @close="closeFn"></addPerson>
  </div>
</template>

<script>
import addPerson from './components/addPerson'
import { getPerson, delPerson, getHolePerson } from '@/api/person'
export default {
  components: {
    addPerson
  },
  data () {
    return {
      isShow: false,
      preBtn: false,
      nextBtn: false,
      totalCount: '',
      totalPage: '',
      pageIndex: 1,
      holePersonList: [],
      personList: [
        {
          num: '',
          userName: '',
          regionName: '',
          role: '',
          mobile: ''
        }
      ],
      perSearchForm: {
        userName: ''
      }
    }
  },
  methods: {
    // *获取人员列表
    async getPerson () {
      const { data } = await getPerson(this.pageIndex)
      // console.log(data)
      this.personList = data.currentPageRecords
      this.personList.forEach((item) => {
        item.role = item.role.roleName
      })
      this.totalCount = data.totalCount
      this.totalPage = data.totalPage
      // this.personList.role = currentPageRecords.role.roleName
      if (this.pageIndex <= 1) {
        this.preBtn = true
      } else {
        this.preBtn = false
      }
      if (this.pageIndex >= this.totalPage) {
        this.nextBtn = true
      } else {
        this.nextBtn = false
      }
    },
    // 顶部搜索框
    search () {
      // 当搜索框内容不为空时，查找名字中包含此字符串
      if (this.perSearchForm.userName) {
        this.personList = this.holePersonList.filter(item => {
          return item.userName.includes(this.perSearchForm.userName)
        })
        // console.log(res)
        this.personList.forEach((item) => {
          item.role = item.role.roleName
        })
        // console.log(this.personList)
      } else {
        this.getPerson()
      }
      this.perSearchForm.userName = ''
    },
    // 底部按钮下一页
    nextPage () {
      this.pageIndex++
      this.getPerson()
    },
    // 底部按钮上一页
    prePage () {
      this.pageIndex--
      this.getPerson()
    },
    // 删除
    async delPerson (id) {
      // console.log(id)
      await this.$confirm('确定删除吗')
      await delPerson(id)
      this.$message.success('删除成功')
      this.getPerson()
    },
    // 关闭弹出框
    closeFn (status) {
      this.isShow = status
    }
  },
  async created () {
    this.getPerson()
    const { data } = await getHolePerson()
    // console.log(data.currentPageRecords)
    this.holePersonList = data.currentPageRecords
  }
}
</script>

<style lang="scss" scoped>
.ry-perList {
  background-color: #f8fafd;
  padding-top: 80px;
  padding-left: 20px;
  .head {
    background-color: #fff;
    width: 100%;
    height: 60px;
    padding-left: 20px;
    padding-top: 10px;
  }
  .body {
    margin-top: 20px;
    width: 100%;
    background-color: #fff;
    padding-left: 20px;
    padding-top: 20px;
    .el-button {
      color: #fff;
      background-color: #ff6927;
      margin-bottom: 20px;
    }
    .list {
      ::v-deep .el-table th {
        background-color: #f3f6fb;
      }
      .change {
        color: blue;
      }
      .del {
        margin-left: 10px;
        color: red;
      }
    }
  }
  .foot {
    width: 100%;
    height: 120px;
    line-height: 60px;
    background-color: #fff;
    padding-left: 30px;
    padding-right: 80px;
    display: flex;
    justify-content: space-between;
    span {
      color: #d8dfe5;
      font-size: 16px;
    }
    .el-button {
      background-color: #d5ddf8;
    }
  }
}
</style>
